﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuickGame.Model;
using DevExpress.Web.ASPxGridView;

namespace QuickGame.View
{
    public partial class QLGame : System.Web.UI.Page
    {
        private ModelContext modelContext;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.modelContext = new ModelContext();
            if (Session["idnguoidung"] == null)
            {
                Response.Redirect("/Account/Login.aspx");
            }
            else
            {
                long idnguoidung = (long)Session["idnguoidung"];
                var nguoidung = NguoiDungHs.QueryEntity(modelContext, idnguoidung);
                if (nguoidung.IDQuyen == 1)
                {
                    this.LoadDL();
                }
                else
                {
                    Response.Redirect("/Account/Login.aspx");
                }
            }
        }
        public void LoadDL()
        {
            this.modelContext = new ModelContext();
            var ds = LuotChoiHs.QueryEntities(modelContext).OrderByDescending(k=>k.NgayBatDau).Select(k => new 
            {
                ID = k.ID,
                Ngay = k.NgayBatDau,
                NguoiChoi = k.NguoiDung.Ten,
                Diem = k.Diem,
                TrangThai = k.TrangThai ==true? "Đang chơi":"Hoàn thành"
            });
            grvds.DataSource = ds;
            grvds.DataBind();
        }

        protected void grvds_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {

        }

        protected void grvChiTiet_Load(object sender, EventArgs e)
        {
            ASPxGridView grvtraloi = sender as ASPxGridView;
            long idluotchoi = (long)grvtraloi.GetMasterRowKeyValue();
            this.LoadCauTraLoi(grvtraloi, idluotchoi);
        }
        public void LoadCauTraLoi(ASPxGridView gridview, long idluotchoi)
        {
            modelContext = new ModelContext();
            LuotChoi luotchoi = LuotChoiHs.QueryEntity(modelContext, idluotchoi);
            var ds = luotchoi.ChiTietChois.Select(k => new
            {
                ID = k.ID,
                CapDo = k.CauHoi.IDCapDo,
                Ngay = k.Ngay,
                CauHoi = k.CauHoi.Ten,
                CauTraLoi = k.CauTraLoi == null ? null : k.CauTraLoi.Ten,
                KetQua = k.CauTraLoi == null ? false : k.CauTraLoi.TrangThai == true ? true : false,
                TroGiup = k.IDTroGiup == 1 ? "50-50" : k.IDTroGiup == 2 ? "Bỏ qua" : k.IDTroGiup == 3 ? "2 Đáp án" : null
            });
            gridview.DataSource = ds;
            gridview.DataBind();
        }
    }
}